package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.Result;
import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.model.*;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.vo.TSOVO;
import com.dongdongshop.vo.TSVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SpecificationServiceImpl implements SpecificationService {
    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;
    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Override
    public List<TSVO> findBrand(TSVO tsvo) {
        TbSpecificationExample tbSpecificationExample = new TbSpecificationExample();
        TbSpecificationExample.Criteria criteria = tbSpecificationExample.createCriteria();
        if (StringUtils.isNotBlank(tsvo.getSpecName())){
            criteria.andSpecNameLike("%"+tsvo.getSpecName()+"%");
        }
        List<TbSpecification> list = tbSpecificationMapper.selectByExample(tbSpecificationExample);
        List<TSVO> collect = list.stream().map(tbSpecification -> {
            TSVO TSVO = new TSVO();
            BeanUtils.copyProperties(tbSpecification, TSVO);
            return TSVO;
        }).collect(Collectors.toList());
        List<TSVO> tsvos = JSONObject.parseArray(JSONObject.toJSONString(list), TSVO.class);
        return tsvos;
    }


    @Override
    public TSVO getSpecById(Long id) {
        //根据id查询数据
        TbSpecification specification = tbSpecificationMapper.selectByPrimaryKey(id);
        if (specification == null) {
            return null;
        }
        //转成vo
        TSVO vo = new TSVO();
        BeanUtils.copyProperties(specification, vo);
        //根据规格id查询选项,一对多关系
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);
        List<TbSpecificationOption> list = tbSpecificationOptionMapper.selectByExample(example);
        //使用lanbom表达式转化
        List<TSOVO> optionVOList = list.stream().map(option -> {
            TSOVO tsovo = new TSOVO();
            BeanUtils.copyProperties(option,tsovo);
            return tsovo;
        }).collect(Collectors.toList());

        vo.setOptionVOList(optionVOList);
        return vo;
    }

    @Override
    public void update(TSVO tsvo) {
        //先修改规格
        TbSpecification tbSpecification = new TbSpecification();
        BeanUtils.copyProperties(tsvo, tbSpecification);
        tbSpecificationMapper.updateByPrimaryKeySelective(tbSpecification);
        //根据规格id删除选项数据
        TbSpecificationOptionExample tbSpecificationOptionExample = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = tbSpecificationOptionExample.createCriteria();
        criteria.andSpecIdEqualTo(tsvo.getId());
        tbSpecificationOptionMapper.deleteByExample(tbSpecificationOptionExample);
        //批量增加规格选项
        List<TSOVO> voList = tsvo.getOptionVOList();
        List<TbSpecificationOption> options = voList.stream().map(vo -> {
            TbSpecificationOption option = new TbSpecificationOption();
            BeanUtils.copyProperties(vo, option);
            option.setSpecId(tsvo.getId());
            return option;
        }).collect(Collectors.toList());
        for (TbSpecificationOption option : options) {
            tbSpecificationOptionMapper.insertSelective(option);
        }
    }



    @Override
    public void DeleteTSVO(Long[] ids) {
        try {
            //删除表一
            TbSpecificationExample example = new TbSpecificationExample();
            TbSpecificationExample.Criteria criteria = example.createCriteria();
            criteria.andIdIn(Arrays.asList(ids));
            tbSpecificationMapper.deleteByExample(example);
            for (Long id : ids) {
                TbSpecificationOptionExample example1 = new TbSpecificationOptionExample();
                TbSpecificationOptionExample.Criteria criteria1 = example1.createCriteria();
                criteria1.andSpecIdEqualTo(id);
                tbSpecificationOptionMapper.deleteByExample(example1);
            }
        }catch (Exception e) {
            throw e;
        }

    }

    @Override
    public void add(TSVO tsovo, List<TbSpecificationOption> list) {
       TbSpecification tb = new TbSpecification();
       BeanUtils.copyProperties(tsovo, tb);
       tbSpecificationMapper.addSpecification(tb);
       //
        for (TbSpecificationOption option : list) {
            option.setSpecId(tb.getId());
            BeanUtils.copyProperties(option, tb);
            tbSpecificationOptionMapper.insertSelective(option);
        }
    }

    @Override
    public List<TbSpecification> list() {
        return tbSpecificationMapper.selectByExample(null);
    }


}
